﻿Public Class ItemsVenta 

    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            'PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
            Panel3.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            'PanelArriba.Enabled = False
            PanelAbajo.Enabled = True
            'Panel3.Enabled = False
        End If
    End Sub

    Private Sub ItemsVenta_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.tipoproducto' table. You can move, or remove it, as needed.
            Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            'Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            'IditemventaTextBox.Visible = False
            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, 5)

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de la Base de Datos")
        End Try
        Panel3.Enabled = False
        detalle.Enabled = False
        ComboBox1.Enabled = False
        DescripcionTextBox.Properties.ReadOnly = True
        NombreTextBox.Properties.ReadOnly = True
        PrecioventaexternoTextBox.Properties.ReadOnly = True
        PrecioventaTextBox.Properties.ReadOnly = True
        ObservacionTextBox.Properties.ReadOnly = True

        'Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(Me.DbFireAndIceDataSet.itemventadetalle, 1)


    End Sub

    Private Sub ItemventadetalleBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItemventadetalleBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.ItemventadetalleBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo guardar los registros en la Base de Datos")
        End Try
    End Sub


    Private Sub IdproductoTextEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdproductoTextEdit.EditValueChanged
        
        IdproductoTextBox.EditValue = IdproductoTextEdit.GetColumnValue("idproducto")
        Descripcion.EditValue = IdproductoTextEdit.GetColumnValue("descripcion")
        CostounitarioTextBox.EditValue = IdproductoTextEdit.GetColumnValue("preciocomprauni")
        IdunidadTextBox.EditValue = IdproductoTextEdit.GetColumnValue("idunidad")
        'CantidadTextBox.EditValue = 0
        TotallineaTextBox.EditValue = 0
        PrecioventaexternoTextBox.EditValue = 0
        PrecioventaTextBox.EditValue = 0


        
    End Sub

    Private Sub CantidadTextBox_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CantidadTextBox.EditValueChanged
        TotallineaTextBox.EditValue = Val(CantidadTextBox.EditValue) * Val(CostounitarioTextBox.EditValue)

    End Sub

 

    Private Sub agregar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click

        BindingNavigatorAddNewItem1.PerformClick()
        detalle.Enabled = True
        Mostrar()
        'ItemventaTableAdapter.InsertQueryItem(Val(IditemventaTextEdit.EditValue), NombreTextBox.Text, DescripcionTextBox.Text, Val(CostototalTextBox.EditValue), Val(PrecioventaTextBox.EditValue), ObservacionTextBox.Text, Val(ComboBox1.SelectedValue), Val(PrecioventaexternoTextBox.EditValue))
        Textventa.EditValue = IdventaTextEdit1.EditValue
        'guardar.Enabled = False
        Panel3.Enabled = False
        detalle.Enabled = True
        ComboBox1.Enabled = True
        DescripcionTextBox.Properties.ReadOnly = False
        NombreTextBox.Properties.ReadOnly = False
        DescripcionTextBox.Properties.ReadOnly = False
        ObservacionTextBox.Properties.ReadOnly = False
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click

        Try
            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(Me.DbFireAndIceDataSet.itemventadetalle, Val(IditemventaTextEdit.EditValue))

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de la Base de Datos")
        End Try
        If NombreTextBox.Text.Length = 0 Then
            ErrorProvider1.SetError(NombreTextBox, "Debe ingresar un nombre para el Item")
        Else
            If ComboBox1.Text = "" Then
                ErrorProvider1.SetError(ComboBox1, "Debe ingresar un Tipo de producto")
            Else
                If PrecioventaTextBox.EditValue = 0 Then
                    ErrorProvider1.SetError(PrecioventaTextBox, "Debe ingresar un Precio Venta")
                Else

                    If PrecioventaexternoTextBox.EditValue = 0 Then
                        ErrorProvider1.SetError(PrecioventaexternoTextBox, "Debe ingresar un Precio Venta 2")
                    Else
                        If detalle.Enabled = False And cancelar.Enabled = False Then

                            Try
                                ItemventaTableAdapter.InsertQueryItem(Val(IditemventaTextEdit.EditValue), NombreTextBox.Text, DescripcionTextBox.Text, Val(CostototalTextBox.EditValue), Val(PrecioventaTextBox.EditValue), ObservacionTextBox.Text, Val(ComboBox1.SelectedValue), Val(PrecioventaexternoTextBox.EditValue), True)

                                Mostrar()
                                Me.ItemventaTableAdapter.Fill(DbFireAndIceDataSet.itemventa)
                            Catch ex As Exception
                                AlertControl1.Show(Owner, "ERROR", "No se pudo guardar registros en la Base de Datos")
                            End Try
                        Else
                            Dim totac As Integer
                            Dim tota As Integer = 1

                            For i As Integer = 0 To GridView2.DataRowCount - 1

                                tota = GridView2.GetRowCellValue(i, "totallinea")
                                totac = totac + tota


                                ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
                            Next i

                            Try
                                ItemventaTableAdapter.UpdateQueryItemVenta(Val(IditemventaTextEdit.EditValue), NombreTextBox.Text, DescripcionTextBox.Text, totac, Val(PrecioventaTextBox.EditValue), ObservacionTextBox.Text, Val(ComboBox1.SelectedValue), Val(PrecioventaexternoTextBox.EditValue), Val(IditemventaTextEdit.EditValue))
                                Mostrar()
                                ErrorProvider1.SetError(ComboBox1, Nothing)
                                ErrorProvider1.SetError(NombreTextBox, Nothing)
                                ErrorProvider1.SetError(PrecioventaexternoTextBox, Nothing)
                                ErrorProvider1.SetError(PrecioventaTextBox, Nothing)
                                Me.ItemventaTableAdapter.Fill(DbFireAndIceDataSet.itemventa)
                                Panel3.Enabled = False
                                detalle.Enabled = False
                                ComboBox1.Enabled = False
                                DescripcionTextBox.Properties.ReadOnly = True
                                NombreTextBox.Properties.ReadOnly = True

                                ObservacionTextBox.Properties.ReadOnly = True
                                Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
                            Catch ex As Exception
                                AlertControl1.Show(Owner, "ERROR", "No se pudo guardar registros en la Base de Datos")
                            End Try

                        End If

                    End If

                End If

            End If


        End If


        
    End Sub

    Private Sub AgregarItemDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgregarItemDetalle.Click
        Textventa.Text = IditemventaTextEdit.EditValue
        If RadioButton2.Checked = True Then
            Dim iditem As Integer
            Dim idpro As Integer
            Dim canti As Integer
            'Dim iduni As Integer
            Dim costu As Integer
            Dim tota As Integer

            For i As Integer = 0 To GridView2.DataRowCount - 1



                iditem = GridView2.GetRowCellValue(i, "iditemventa")
                idpro = GridView2.GetRowCellValue(i, "idproducto")

                costu = GridView2.GetRowCellValue(i, "costounitario")
                tota = GridView2.GetRowCellValue(i, "totallinea")
                canti = GridView2.GetRowCellValue(i, "cantidad")

                Try
                    ItemventadetalleTableAdapter.InsertQueryItemdetalle(Textventa.EditValue, idpro, canti, costu, tota)
                    'ItemventadetalleTableAdapter.Fill(DbFireAndIceDataSet.itemventadetalle)
                Catch ex As Exception
                    AlertControl1.Show(Owner, "ERROR", "No se pudo insertar registros en la Base de Datos")
                End Try
                ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
            Next i
        Else
            If CantidadTextBox.Text.Length = 0 Then
                ErrorProvider1.SetError(CantidadTextBox, "Debe ingresar un nombre para el Documento")
            Else
                If PrecioventaexternoTextBox.Text.Length = 0 Then
                    ErrorProvider1.SetError(PrecioventaexternoTextBox, "Debe ingresar un nombre para el Documento")
                Else
                    If PrecioventaTextBox.Text.Length = 0 Then
                        ErrorProvider1.SetError(PrecioventaTextBox, "Debe ingresar un nombre para el Documento")
                    Else
                        ErrorProvider1.SetError(PrecioventaTextBox, Nothing)
                        ErrorProvider1.SetError(PrecioventaexternoTextBox, Nothing)
                        ErrorProvider1.SetError(CantidadTextBox, Nothing)
                        Try

                            TotallineaTextBox.EditValue = Val(CantidadTextBox.EditValue) * Val(CostounitarioTextBox.EditValue)

                            'Dim existe As Boolean = False
                            'Dim total As Decimal
                            ' Dim cantidad As Decimal
                            'Dim colum As Integer

                            '  If GridView1.RowCount = 0 Then
                            'MsgBox("vacio")
                            CostototalTextBox.EditValue = Val(TotallineaTextBox.EditValue) + Val(CostototalTextBox.EditValue)
                            'MsgBox(CostototalTextBox.Text)
                            ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.EditValue), Val(IdproductoTextBox.EditValue), Val(CantidadTextBox.EditValue), Val(CostounitarioTextBox.EditValue), Val(TotallineaTextBox.EditValue))
                            'ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, IditemventaTextEdit.EditValue)
                            ' Else

                        Catch ex As Exception
                            AlertControl1.Show(Owner, "ERROR", "No se pudo guardar registros en la Base de Datos")
                        End Try



                        'End If
                        '       Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
                    End If
                End If

            End If


        End If
        guardar.Enabled = True

    End Sub

   

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        ' For I As Integer = 0 To GridView1.RowCount - 1
        'If GridView1.GetRowCellValue(I, "idproducto") = IdproductoTextBox.Text Then
        'MsgBox("Esta Duplicado")
        'End If
        'Next
        For i As Integer = 0 To GridView1.DataRowCount - 1
            ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))
        Next i
        Try

            ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)

            Mostrar()
            Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
        End Try
        Panel3.Enabled = False
        detalle.Enabled = False
        ComboBox1.Enabled = False
        DescripcionTextBox.Properties.ReadOnly = True
        NombreTextBox.Properties.ReadOnly = True
        DescripcionTextBox.Properties.ReadOnly = True
        ObservacionTextBox.Properties.ReadOnly = True
    End Sub


    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
        ItemsVenta2.ShowDialog()
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try

                ' UnidadescategoriaTableAdapter.DeleteQuery(IdunidadescategoriaTextBox.Text)
                For i As Integer = 0 To GridView1.DataRowCount - 1
                    ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))
                Next i

                ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)
                Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
                Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
                Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            Catch ex As Exception
                AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
            End Try


        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text = "" Then

        End If
    End Sub

   

    Private Sub SimpleButton5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles detalle.Click
        
            If NombreTextBox.Text.Length = 0 Then
                ErrorProvider1.SetError(NombreTextBox, "Debe ingresar un nombre para el Item")
            Else
                If ComboBox1.Text = "" Then
                    ErrorProvider1.SetError(ComboBox1, "Debe ingresar un Tipo de producto")
                Else

                    ErrorProvider1.SetError(ComboBox1, Nothing)
                    ErrorProvider1.SetError(NombreTextBox, Nothing)
                Panel3.Enabled = True
                PrecioventaexternoTextBox.EditValue = 0
                PrecioventaTextBox.EditValue = 0
                CostototalTextBox.EditValue = 0
                Try
                    ItemventaTableAdapter.InsertQueryItem(Val(IditemventaTextEdit.EditValue), NombreTextBox.Text, DescripcionTextBox.Text, Val(CostototalTextBox.EditValue), Val(PrecioventaTextBox.EditValue), ObservacionTextBox.Text, Val(ComboBox1.SelectedValue), Val(PrecioventaexternoTextBox.EditValue), True)
                    detalle.Enabled = False
                    ItemsVenta2.ShowDialog()
                Catch ex As Exception
                    AlertControl1.Show(Owner, "ERROR", "No se pudo insertar registros en la Base de Datos")
                End Try
                'MessageBox.Show("Se guardó el resgistro satisfactoriamente")
            End If
            End If

        RadioButton1.Checked = True

    End Sub

    Private Sub ItemsVenta_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

        If cancelar.Enabled = True Then
            Try


                For i As Integer = 0 To GridView1.DataRowCount - 1
                    ItemventadetalleTableAdapter.DeleteQueryItemdetalle(Val(IditemventaTextEdit.Text))
                Next i

                ItemventaTableAdapter.DeleteQueryItem(IditemventaTextEdit.EditValue)

            Catch ex As Exception
                AlertControl1.Show(Owner, "ERROR", "No se pudo eliminar registros de la Base de Datos")
            End Try
            Mostrar()
            Try
                Me.TipoproductoTableAdapter.Fill(Me.DbFireAndIceDataSet.tipoproducto)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
                Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
                'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
                Me.ItemventadetalleTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventadetalle)
            Catch ex As Exception
                AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
            End Try
        End If
    End Sub

   
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            RadioButton2.Checked = False
            IdproductoTextEdit.Visible = True
            IdventaTextEdit1.Visible = False
            Label1.Text = "Busqueda de Productos"
            Descripcion.Properties.ReadOnly = False
            CantidadTextBox.Properties.ReadOnly = False

            CostounitarioTextBox.Properties.ReadOnly = True
            CantidadTextBox.Properties.ReadOnly = False
            IdunidadTextBox.Properties.ReadOnly = True
            PrecioventaTextBox.Properties.ReadOnly = False
            PrecioventaexternoTextBox.Properties.ReadOnly = False
            CostototalTextBox.Properties.ReadOnly = True
            TotallineaTextBox.Properties.ReadOnly = True
            PrecioventaexternoTextBox.EditValue = 0
            PrecioventaTextBox.EditValue = 0
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            RadioButton1.Checked = False

            IdproductoTextEdit.Visible = False
            IdventaTextEdit1.Visible = True
            Label1.Text = "Busqueda de Items"
            CantidadTextBox.Properties.ReadOnly = True
            
            IdunidadTextBox.Properties.ReadOnly = True

            TotallineaTextBox.Properties.ReadOnly = True
            CostototalTextBox.Properties.ReadOnly = True
            CostounitarioTextBox.Properties.ReadOnly = True
            Descripcion.Properties.ReadOnly = True
        End If
    End Sub

    Private Sub IdventaTextEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdventaTextEdit1.EditValueChanged
        Dim codigo As Integer
        Try
            codigo = IdventaTextEdit1.GetColumnValue("iditemventa")
            Descripcion.EditValue = IdventaTextEdit1.GetColumnValue("descripcion")

            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(Me.DbFireAndIceDataSet.itemventadetalle, codigo)

        Catch ex As System.Exception
            'System.Windows.Forms.MessageBox.Show(ex.Message)
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
        End Try
    End Sub

    Private Sub FillQueryItemdetalleToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillQueryItemdetalleToolStripButton.Click
        Try
            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(Me.DbFireAndIceDataSet.itemventadetalle, CType(PARAM1ToolStripTextBox.Text, Decimal))
        Catch ex As System.Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
        End Try

    End Sub



    Private Sub SimpleButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Try
            ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, 1)

        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros en la Base de Datos")
        End Try
    End Sub

   
End Class